#include "resistor.h"

#include <QPainter>
#include <QPainterPath>

Resistor::Resistor(QGraphicsItem *parent)
    : CircuitComponent(parent)
{
    setFlag(QGraphicsItem::ItemIsSelectable);
}

QPointF Resistor::inputPort() const
{
    return QPointF(-35, 0);
}

QPointF Resistor::outputPort() const
{
    return QPointF(35, 0);
}

void Resistor::setText(const QString &text)
{
    m_text = text;
}

QRectF Resistor::boundingRect() const
{
    return QRectF(-40, -15, 80, 30);
}

void Resistor::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
    Q_UNUSED(option);
    Q_UNUSED(widget);

    painter->setRenderHint(QPainter::Antialiasing);

    // 绘制导线
    painter->setPen(QPen(Qt::red, 2));
    painter->drawLine(-35, 0, -25, 0);
    painter->drawLine(25, 0, 35, 0);    

    // 绘制电阻波浪线
    QPainterPath path;
    path.moveTo(-25, 0);
    for(int i = -20; i <= 20; i += 10) {
        path.lineTo(i, (i % 20 == 0) ? -10 : 10);
    }
    path.lineTo(25, 0);

    painter->strokePath(path, QPen(Qt::red, 2));

    painter->setPen(QPen(Qt::white));
    //绘制文字
    painter->drawText(QRect(-30, -30, 80, 20), Qt::AlignLeft,m_text);
}
